package com.youli.gb.service.impl;

import java.util.List;

import com.youli.gb.mapper.SysRoleMapper;
import com.youli.gb.mapper.SysUserMapper;
import com.youli.gb.model.SysRole;
import com.youli.gb.model.SysUser;
import com.youli.gb.service.ISysRoleService;
import com.youli.gb.utils.DBUtils;

public class SysRoleServiceImpl implements ISysRoleService {

	private SysRoleMapper roleMapper = DBUtils.getSqlSession().getMapper(SysRoleMapper.class);
	private SysUserMapper userMapper = DBUtils.getSqlSession().getMapper(SysUserMapper.class);

	@Override
	public SysRole getById(int id) {
		SysRole role = roleMapper.getById(id);
		return role;
	}

	@Override
	public List<SysRole> getAll() {
		return roleMapper.getAll();
	}

	@Override
	public int add(String roleName, String description) {
		SysRole sysRole = new SysRole(roleName, description);
		return this.roleMapper.add(sysRole);
	}

	@Override
	public SysRole getByRoleName(String roleName) {
		return this.roleMapper.getByRoleName(roleName);
	}

	@Override
	public int deleteById(Integer id) {
		//获取与role id对应的角色在用户表中的关联数据
		List<SysUser> userlList = this.userMapper.getAllByRoleId(id);
		if (userlList != null && userlList.size() > 0) {
			return -1;	
		}
		return this.roleMapper.deleteById(id);
	}

	@Override
	public int deleteByIds(String[] ids) {		
		return this.roleMapper.deleteByIds(ids);
	}

	@Override
	public int edit(Integer id, String description) {
		return this.roleMapper.edit(id, description);
	}
	
}
